/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.editor.message;


import org.eclipse.uml2.uml.CombinedFragment;
import org.eclipse.uml2.uml.Gate;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.InteractionOperand;
import org.eclipse.uml2.uml.InteractionUse;
import org.eclipse.uml2.uml.MessageEnd;
import org.eclipse.uml2.uml.MessageOccurrenceSpecification;
import org.eclipse.uml2.uml.NamedElement;

import com.cea.papyrus.diagram.sequence.sedi.model.command.EventPosition;
import com.cea.papyrus.diagram.sequence.sedi.model.command.LifelineMessageEndPosition;
import com.cea.papyrus.diagram.sequence.sedi.model.command.MessageEndPosition;

/**
 * A utility class that facilitates work with UML messages.
 * <p>
 * Instances of this class cannot not be created directly. There are several kinds of
 * message helpers, according to the type of the UML element the messages relate to. To
 * get an instance that is able to work with messages for a specific UML element, call
 * {@link #select(Object)}.
 * </p>
 * 
 * @author frankd
 */
public abstract class MessageHelper {

    /**
     * Returns the interaction with which the given element is associated.
     */
    public abstract Interaction getInteraction(MessageEndPosition position);

    /**
     * Create and adds a new MessageEnd on the given element. The new MessageEnd will be
     * positioned as specified by the given 'position'.
     */
    public abstract MessageEnd createMessageEnd(MessageEndPosition position, boolean send);

    /**
     * Adds the given MessageEnd at the specified position.
     */
    public abstract void addMessageEnd(MessageEndPosition position, MessageEnd end, boolean send);

    public abstract void sendMessage(MessageEnd end, NamedElement signature);

    public abstract void receiveMessage(MessageEnd end, NamedElement signature);

    public abstract EventPosition removeMessage(MessageEnd end);

    /**
     * Deletes the given MessageEnd from the given end. Returns the Element immediately in
     * front of the deleted MessageEnd in case of Undo.
     */
    public abstract MessageEndPosition removeMessageEnd(MessageEnd end);

    MessageHelper() {
        // not public to discourage external instantiation
    }

    /**
     * Returns a MessageHelper suitable for the specified element.
     * 
     * @param element
     *            An object that specifies what the helper will work on. Supported element
     *            types are positions on Lifelines, Interactions, InteractionUses and
     *            CombinedFragments (see
     *            {@link com.cea.papyrus.diagram.sequence.sedi.model.command.MessageEndPosition} and its
     *            subclasses}, as well as the various kinds of MessageEnds themselves.
     * @return a suitable helper.
     * @throws IllegalArgumentException
     *             if the element is not supported.
     */
    public static MessageHelper select(Object element) {
        if (element instanceof LifelineMessageEndPosition)
            return new LifelineMessageHelper();
        else if (element instanceof MessageEndPosition) {
            MessageEndPosition pos = (MessageEndPosition) element;
            return selectFromFragment(pos.getOwner());
        } else if (element instanceof MessageOccurrenceSpecification)
            return new LifelineMessageHelper();
        else if (element instanceof Gate) {
            Gate gate = (Gate) element;
            return selectFromFragment(gate.getOwner());
        }
        throw new IllegalArgumentException("Unsupported selector: " + element);
    }

    private static MessageHelper selectFromFragment(Object o) {
        if (o instanceof Interaction)
            return new InteractionMessageHelper();
        else if (o instanceof InteractionUse)
            return new InteractionUseMessageHelper();
        else if (o instanceof InteractionOperand || o instanceof CombinedFragment)
            return new CombinedFragmentMessageHelper();
        throw new IllegalArgumentException("Unsupported selector: " + o);
    }
}
